package cn.itsy.web.controller;

import cn.itsy.dao.position.EmpJobDuctEmpstatusDao;
import cn.itsy.pojo.Dept;
import cn.itsy.pojo.Duct;
import cn.itsy.pojo.EmpJobDuctEmpstatus;
import cn.itsy.pojo.Job;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * sj
 * 09点26分
 * 辞退controller
 * EmpJobDuctEmpstatus
 */
@RequestMapping("XiaoJianEmpControllerCt")
@Controller
public class XiaoJianEmpControllerCt {
    @Resource
    private EmpJobDuctEmpstatusDao empimpl;

    /**
     * 查询所有
     *
     * @param emp
     * @return
     */
    @RequestMapping("selectEmpJobDuctEmpstatus")
    @ResponseBody
    public List<EmpJobDuctEmpstatus> selectEmpJobDuctEmpstatusShow(EmpJobDuctEmpstatus emp, @RequestParam Map<String, Object> map) {
        System.out.println("i:" + map.get("i") + "----state:" + map.get("state") + "-----reverse:" + map.get("reverse"));
        System.out.println("进入查询方法");
        System.out.println("名字：" + emp.getEmpname());
        System.out.println("职位：" + emp.getName());
        System.out.println("性别：" + emp.getSex());
        System.out.println("职称：" + emp.getZname());
        System.out.println("部门：" + emp.getDname());
        System.out.println("学历：" + emp.getEducation());

        String sql = "select em.*,j.name,d.zname,dep.dname from emp em,job j,duct d,empstatus esta, dept dep " +
                " where em.job_id=j.id  and em.duct_id=d.id and em.is_status=esta.id and em.depart_id=dep.id and esta.id='1'";
        /*名字模糊匹配*/
        if (emp.getEmpname() != null && !emp.getEmpname().equals("")) {
            System.out.println("名字模糊匹配");
            sql += " and  em.empname like '%" + emp.getEmpname() + "%' ";
        }
        /*性别查询*/
        if (emp.getSex() != null && !emp.getSex().equals("")) {
            System.out.println("性别查询");
            sql += " and em.sex='" + emp.getSex() + "' ";
        }
        /*学历查询*/
        if (emp.getEducation() != null && !emp.getEducation().equals("")) {
            System.out.println("学历查询");
            sql += " and em.education='" + emp.getEducation() + "' ";
        }
        /*部门查询*/
        if (emp.getDname() != null && !emp.getDname().equals("")) {
            System.out.println("部门查询");
            sql += " and dep.id='" + emp.getDname() + "' ";
        }
        /*职位查询 job职称 dupt职位*/
        if (emp.getName() != null && !emp.getName().equals("")) {
            System.out.println("-------------------------------------------职位查询！！！！！！！！！！");
            sql += " and d.id='" + emp.getName() + "' ";
        }
        /*职称查询 job职称 dupt职位*/
        if (emp.getZname() != null && !emp.getZname().equals("")) {
            System.out.println("-------------------------------------------职称查询！！！！！！！！！！");
            sql += " and j.id='" + emp.getZname() + "' ";
        }
        //查询出有多少条数据最终完成分页
        List<EmpJobDuctEmpstatus> empsize = empimpl.selectEmpJobDuctEmpstatusShow(sql);
        /**
         * sj
         * 分页
         */
        //第几页
        Integer is = Integer.valueOf(map.get("i").toString());
        //每页显示多少行
        Integer isum = 10;
        //总页数
        Integer count = empsize.size() % isum > 0 ? empsize.size() / isum + 1 : empsize.size() / isum;

        //判断点击的是哪一个按钮
        if (map.get("state").equals("首页")) {
            is = 1;
        }
        if (map.get("state").equals("上一页")) {
            is = is <= 1 ? 1 : is - 1;
        }
        if (map.get("state").equals("下一页")) {
            is = is >= count ? count : is + 1;
        }
        if (map.get("state").equals("尾页")) {
            is = count;
        }
        if (map.get("state").equals("跳转")) {
            if (map.get("reverse") != null && !map.get("reverse").equals("")) {
                System.out.println(map.get("reverse") + "=-=-=-=-=-=-=-=-=-==-=-=");
                Integer a = new Integer((String) map.get("reverse"));
                if (a > count) {
                    is = count;
                }
                if (a < count) {
                    is = Integer.valueOf(map.get("reverse").toString());
                }
            }
        }
        int isx = (is - 1) * isum;
        sql += " limit " + isx + "," + isum + " ";

        System.out.println(sql);
        List<EmpJobDuctEmpstatus> empJobDuctEmpstatuses = empimpl.selectEmpJobDuctEmpstatusShow(sql);
        System.out.println("list集合：" + empJobDuctEmpstatuses.toString());
        String sqlselectdept = "select d.id,d.dname from dept d";
        List<Dept> listdept = empimpl.selectDeptShow(sqlselectdept);
        System.out.println("listdept：" + listdept.toString());
        return empJobDuctEmpstatuses;
    }

    /**
     * tr点击事件
     * 按照id查询
     *
     * @param emp
     * @return
     */
    @RequestMapping("trclickFunction")
    @ResponseBody
    public List<EmpJobDuctEmpstatus> showWW(EmpJobDuctEmpstatus emp) {
        String sql = "select em.*,j.name,d.zname,dep.dname from emp em,job j,duct d,empstatus esta, dept dep " +
                " where em.job_id=j.id  and em.duct_id=d.id and em.is_status=esta.id and em.depart_id=dep.id and esta.id='1' and em.empid='" + emp.getEmpid() + "'";
        List<EmpJobDuctEmpstatus> empJobDuctEmpstatuses = empimpl.selectEmpJobDuctEmpstatusShow(sql);
        return empJobDuctEmpstatuses;
    }

    /**
     * 查询dept部门
     *
     * @param dept
     * @return
     */
    @RequestMapping("selectDept")
    @ResponseBody
    public List<Dept> selectDeptShow(Dept dept) {
        System.out.println("查询部门");
        String sql = "select d.id,d.dname from dept d";
        List<Dept> listdept = empimpl.selectDeptShow(sql);
        System.out.println("部门表List：" + listdept.toString());
        return listdept;
    }

    /**
     * 查询职位表
     *
     * @param
     * @return job职称 dupt职位
     */
    @RequestMapping("selectJob")
    @ResponseBody
    public List<Job> selectJobShow(Job jo) {
        System.out.println("查询职位");
        String sql = "select * from job";
        List<Job> listjob = empimpl.selectJobShow(sql);
        System.out.println("职位表List：" + listjob.toString());
        return listjob;
    }

    /**
     * 查询duct职称表
     *
     * @param duct
     * @return
     */
    @RequestMapping("selectDuct")
    @ResponseBody
    public List<Duct> selectDuctShow(Duct duct) {
        System.out.println("查询职称");
        String sql = "select * from duct";
        List<Duct> listduct = empimpl.selectDuctShow(sql);
        System.out.println("职称表List：" + listduct.toString());
        return listduct;
    }

    /*辞退a标签请求*/
    @RequestMapping("jspShow")
    public String jspShow(EmpJobDuctEmpstatus emp, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("nice!" + emp.toString());
        System.out.println("id" + emp.getEmpid());
        String sql = "select em.*,j.name,d.zname,dep.dname from emp em,job j,duct d,empstatus esta, dept dep " +
                " where em.job_id=j.id  and em.duct_id=d.id and em.is_status=esta.id and em.depart_id=dep.id and esta.id='1' and em.empid='" + emp.getEmpid() + "'";
        List<EmpJobDuctEmpstatus> empJobDuctEmpstatuses = empimpl.selectEmpJobDuctEmpstatusShow(sql);
        System.out.println("jsp empJobDuctEmpstatuses:" + empJobDuctEmpstatuses.toString());
        request.setAttribute("empJobDuctEmpstatuses", empJobDuctEmpstatuses);
        return "/ciTuiUpdate";
    }

    /*添加辞退历史记录   修改状态为4*/
    @RequestMapping("updateemp")                                    //txt文本域的值
    public String updateShow(EmpJobDuctEmpstatus emp, @RequestParam String txt) {
        System.out.println("emp集合:" + emp.toString());
        //当前时间
        Date date = new Date();
        String sj = date.toLocaleString();
        String str = "辞退";
        //往emphistory表中添加辞职记录
        String insertsql = "insert into emphistory values(default,'" + emp.getId() + "','" + sj + "','" + txt + "','" + str + "')";
        empimpl.insertEmpHistoryShow(insertsql);
        //修改emp表中的is_status字段（状态）
        String updatesql = "update emp set is_status='" + 4 + "' where id='" + emp.getId() + "'";
        empimpl.updateEmpHistoryShow(updatesql);

        return "redirect:/citui.jsp";
    }


}
